package xin.clips.web.controller.gy;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.annotation.Log;
import xin.clips.common.enums.BusinessType;
import xin.clips.common.utils.ShiroUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.GyKnowBaseType;
import xin.clips.system.domain.gy.GyUserDoc;
import xin.clips.system.service.gy.IGyUserDocService;
import xin.clips.common.core.controller.BaseController;
import xin.clips.common.core.domain.AjaxResult;
import xin.clips.common.utils.poi.ExcelUtil;
import xin.clips.common.core.page.TableDataInfo;

/**
 * 用户文档Controller
 *
 * @author clips
 * @date 2023-02-28
 */
@Controller
@RequestMapping("/gy/userDoc")
public class GyUserDocController extends BaseController {
    private String prefix = "gy/userDoc";

    @Autowired
    private IGyUserDocService gyUserDocService;


    @Value(value = "${hostInfo}")
    private String hostInfo;

    @RequiresPermissions("gy:userDoc:view")
    @GetMapping()
    public String doc() {
        return prefix + "/doc";
    }

    @RequiresPermissions("gy:userDoc:docShare")
    @GetMapping("/docShare/{fileId}")
    public String docShare(@PathVariable("fileId") String fileId, ModelMap mmap) {
        mmap.put("userDoc", gyUserDocService.selectGyUserDocByFileId(fileId));
        mmap.put("hostInfo", hostInfo);
        return prefix + "/docShare";
    }

    /**
     * 查询用户文档列表
     */
    @RequiresPermissions("gy:userDoc:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GyUserDoc gyUserDoc) {
        startPage();
        gyUserDoc.setUserId(ShiroUtils.getUserId());
        List<GyUserDoc> list = gyUserDocService.selectGyUserDocList(gyUserDoc);
        return getDataTable(list);
    }

    /**
     * 新增用户文档
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") String parentId, ModelMap mmap) {
        mmap.put("parentId", parentId);
        return prefix + "/add";
    }


    /**
     * 新增用户文档
     */
    @GetMapping("/upload/{parentId}")
    public String upload(@PathVariable("parentId") String parentId, ModelMap mmap) {
        mmap.put("parentId", parentId);
        return prefix + "/upload";
    }


    /**
     * 新增保存用户文档
     */
    @RequiresPermissions("gy:userDoc:add")
    @Log(title = "用户文档", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GyUserDoc gyUserDoc) {
        return gyUserDocService.insertGyUserDoc(gyUserDoc);
    }

    /**
     * 新增保存用户文档
     */
    @RequiresPermissions("gy:userDoc:view")
    @PostMapping("/daohang")
    @ResponseBody
    public AjaxResult daohang(GyUserDoc gyUserDoc, ModelMap mmap) {
        List<String> filesInfo = new ArrayList<>();
        GyUserDoc userDoc = gyUserDocService.selectGyUserDocByFileId(gyUserDoc.getFileId()); //根据文件id获取文件信息
        if (userDoc.getParentId().equals("0")) {          //是一级菜单，直接返回 如： 我的文档  >  文件夹1
            filesInfo.add(userDoc.getFileName() + "," + userDoc.getFileId());
            return success(filesInfo);
        }
        //有多级，根据ancestors获取返回
        List<GyUserDoc> list = gyUserDocService.selectGyUserDocByFileIds(userDoc.getAncestors());
        for (GyUserDoc temp : list) {
            //if(temp.getParentId().equals("0"))continue;
            filesInfo.add(temp.getFileName() + "," + temp.getFileId());
        }
       // Collections.reverse(filesInfo);
        return success(filesInfo);
    }

    private GyUserDoc getUserDocByParentId(String fileId) {
        return gyUserDocService.selectGyUserDocByParentId(fileId);
    }

    /**
     * 修改用户文档
     */
    @RequiresPermissions("gy:userDoc:add")
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") String fileId, ModelMap mmap) {
        GyUserDoc gyUserDoc = gyUserDocService.selectGyUserDocByFileId(fileId);
        mmap.put("userDoc", gyUserDoc);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户文档
     */
    @RequiresPermissions("gy:userDoc:add")
    // @Log(title = "用户文档", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GyUserDoc gyUserDoc) {

        return toAjax(gyUserDocService.updateGyUserDoc(gyUserDoc));
    }

    /**
     * 删除知识库类型
     */
    @RequiresPermissions("gy:userDoc:remove")
    // @Log(title = "用户文档", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable(value = "id", required = true) String id) {
        GyUserDoc userDoc= new GyUserDoc();
        userDoc.setParentId(id);
        List<GyUserDoc> list1  = gyUserDocService.selectGyUserDocList(userDoc);
        if(list1!=null && list1.size()>0){
            return error("存在下级文档，不能删除!");
        }
        GyUserDoc doc = gyUserDocService.selectGyUserDocByFileId(id);
        if (doc.getFileType().toString().equals("0")) {
            //是文件夹，查看下面有没有文件，有的话提示
            GyUserDoc childDoc = new GyUserDoc();
            childDoc.setParentId(doc.getFileId());
            List<GyUserDoc> list = gyUserDocService.selectGyUserDocList(childDoc);
            if (CollUtil.isNotEmpty(list)) {
                return error("该文件夹下存在文件，不能删除!");
            }
        }
        return toAjax(gyUserDocService.deleteGyUserDocByFileId(id));
    }
}
